# coding: utf-8
from typing import Union, List, Dict

from PySide6.QtCore import Qt
from PySide6.QtGui import QPixmap, QImage
from PySide6.QtWidgets import QHBoxLayout, QVBoxLayout, QWidget
from qfluentwidgets import CardWidget, BodyLabel, setFont, VerticalSeparator, PrimaryPushButton, PushButton, \
    FluentIcon as FIF, themeColor

from common import OthersIcon, Icon
from components import CentreImageLabel, SongPushButton


class SongInfoCard(CardWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.horizontalLayout = QHBoxLayout(self)
        self.verticalLayout = QVBoxLayout()
        self.horizontalLayout_2 = QHBoxLayout()
        self.horizontalLayout_3 = QHBoxLayout()

        self.centerLabel = CentreImageLabel(OthersIcon.Default.path(), self)
        self.songLabel = BodyLabel('歌曲', self)
        self.songNameLabel = BodyLabel('歌曲名', self)
        self.singerLabel = BodyLabel('歌手', self)
        self.songLengthLabel = BodyLabel('00:00', self)
        self.verticalSeparator = VerticalSeparator(self)
        self.timeLabel = BodyLabel('2022-05-08', self)
        self.playButton = PrimaryPushButton(FIF.PLAY, '立即播放', self)
        self.collectButton = PushButton(Icon.Collect, '收藏', self)
        self.downloadButton = PushButton(FIF.DOWNLOAD, '下载歌曲', self)

        self.__initWidgets()

    def __initWidgets(self):
        self.centerLabel.setFixedSize(323, 323)
        self.centerLabel.setBorderRadius(5, 5, 5, 5)
        self.playButton.setFixedSize(193, 48)
        self.collectButton.setFixedSize(193, 48)
        self.downloadButton.setFixedSize(193, 48)

        setFont(self.songLabel, 24)
        setFont(self.songNameLabel, 48)
        setFont(self.singerLabel, 24)
        setFont(self.songLengthLabel, 24)
        setFont(self.timeLabel, 24)
        self.horizontalLayout.setContentsMargins(40, 40, 40, 40)
        self.horizontalLayout.setSpacing(77)
        self.verticalLayout.setSpacing(16)
        self.verticalLayout.setAlignment(Qt.AlignmentFlag.AlignLeft | Qt.AlignmentFlag.AlignVCenter)
        self.horizontalLayout_2.setAlignment(Qt.AlignmentFlag.AlignLeft)
        self.horizontalLayout_3.setAlignment(Qt.AlignmentFlag.AlignLeft)
        self.horizontalLayout.addWidget(self.centerLabel)
        self.verticalLayout.addWidget(self.songLabel)
        self.verticalLayout.addWidget(self.songNameLabel)
        self.verticalLayout.addWidget(self.singerLabel)
        self.horizontalLayout_2.addWidget(self.songLengthLabel)
        self.horizontalLayout_2.addWidget(self.verticalSeparator)
        self.horizontalLayout_2.addWidget(self.timeLabel)
        self.verticalLayout.addStretch(1)
        self.verticalLayout.addLayout(self.horizontalLayout_2)
        self.horizontalLayout_3.addWidget(self.playButton)
        self.horizontalLayout_3.addWidget(self.collectButton)
        self.horizontalLayout_3.addWidget(self.downloadButton)
        self.verticalLayout.addStretch(1)
        self.verticalLayout.addLayout(self.horizontalLayout_3)
        self.horizontalLayout.addLayout(self.verticalLayout)

    def setSongName(self, name: str):
        """
        设置歌曲名
        :param name:
        :return:
        """
        self.songNameLabel.setText(name)

    def setSingerName(self, name: str):
        """
        设置歌手名
        :param name:
        :return:
        """
        self.singerLabel.setText(name)

    def setSongLength(self, length: str):
        """
        设置歌曲长度
        :param length:
        :return:
        """
        self.songLengthLabel.setText(length)

    def setTime(self, time: str):
        """
        设置歌曲时间
        :param time:
        :return:
        """
        self.timeLabel.setText(time)

    def setSongImage(self, image: Union[str, QPixmap, QImage]):
        """
        设置歌曲图片
        :param image:
        :return:
        """
        self.centerLabel.setImage(image)

    def setSongInfo(self, image: Union[str, QPixmap, QImage], name: str, singer: str, length: str, time: str):
        """
        设置歌曲信息
        :param image: 歌曲图片
        :param name: 歌曲名
        :param singer: 歌手名
        :param length: 歌曲长度
        :param time: 歌曲时间
        :return:
        """
        self.setSongImage(image)
        self.setSongName(name)
        self.setSingerName(singer)
        self.setSongLength(length)
        self.setTime(time)


class RecommendWidget(QWidget):
    """
    推荐歌曲卡片
    """

    def __init__(self, parent=None):
        super().__init__(parent)
        self.verticalLayout = QVBoxLayout(self)
        self.bodyLabel = BodyLabel(self)

        self.bodyLabel.setTextColor(themeColor(), themeColor())
        setFont(self.bodyLabel, 31)

        self.verticalLayout.setAlignment(Qt.AlignmentFlag.AlignTop)
        self.verticalLayout.setSpacing(8)
        self.verticalLayout.setContentsMargins(0, 0, 0, 0)
        self.verticalLayout.addWidget(self.bodyLabel)

    def addSongInfo(self, image: Union[str, QPixmap, QImage], name: str, singer: str, clicked=None):
        button = SongPushButton(image, name, singer, self)
        button.setFixedHeight(70)
        if clicked:
            button.clicked.connect(clicked)
        self.verticalLayout.addWidget(button)

    def setTitle(self, title: str):
        self.bodyLabel.setText(title)


class RecommendedSongCard(CardWidget):
    """
    推荐歌曲卡片
    """

    def __init__(self, parent=None):
        super().__init__(parent)
        self.verticalLayout = QVBoxLayout(self)
        self.setFixedWidth(400)
        self.verticalLayout.setSpacing(30)
        self.verticalLayout.setContentsMargins(20, 10, 20, 10)

    def addCard(self, title: str, songInfo: List[Dict[str, str]]):
        card = RecommendWidget(self)
        card.setTitle(title)
        for info in songInfo:
            card.addSongInfo(**info)
        self.verticalLayout.addWidget(card)
